package com.sx.zongzhi.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.sx.zongzhi.BaseActivity;
import com.sx.zongzhi.R;
import com.sx.zongzhi.adapter.PictureAdapter;
import com.sx.zongzhi.bean.AppealBean;
import com.sx.zongzhi.bean.AppealDetailBean;
import com.sx.zongzhi.presenter.AppealDetailPest;
import com.sx.zongzhi.utils.ToastUtils;
import com.sx.zongzhi.view.IAppealDetailView;
import com.sx.zongzhi.voiceamr.Player;
import com.sx.zongzhi.widget.CustomDialog;
import com.sx.zongzhi.widget.LoadDialog;

import butterknife.BindView;
import butterknife.ButterKnife;

import static com.sx.zongzhi.MyApplication.context;

/**
 * 矛盾纠纷详情
 */
public class AppealDetailActivity extends BaseActivity implements IAppealDetailView{

    @BindView(R.id.tv_EventName) TextView tv_EventName;
    //发生网格
    @BindView(R.id.tv_HappenGrid) TextView tv_HappenGrid;
    @BindView(R.id.tv_HappenLocation) TextView tv_HappenLocation;
    //发生日期
    @BindView(R.id.tv_Data) TextView tv_Data;
    //主要当事人
    @BindView(R.id.tv_Litigant) TextView tv_Litigant;
    @BindView(R.id.tv_Phone) TextView tv_Phone;
    @BindView(R.id.tv_AppealSource) TextView tv_AppealSource;
    @BindView(R.id.tv_AppealType) TextView tv_AppealType;
    @BindView(R.id.tv_Describe) TextView tv_Describe;
    @BindView(R.id.rv_imageRV) RecyclerView rvImageRV;
    private AppealDetailPest detailPest;
    /**事件主键*/
    private AppealBean.RowsBean rowsBean;
    private Player player;

    @Override
    public void setUI(Bundle savedInstanceState) {
        setContentView(R.layout.activity_appeal_detail);
        setTitleName("诉求服务详情");
        rowsBean= (AppealBean.RowsBean) getIntent().getSerializableExtra("AppealBean.RowsBean");
        ButterKnife.bind(this);
        initData();
    }

    private void initData() {
        detailPest=new AppealDetailPest(this);
        if (rowsBean!=null){
            LoadDialog.show(this);
            detailPest.getAppealDetail(rowsBean.getAppealId(),rowsBean.getSourcesWay(),rowsBean.getStepRecordId());
        }
    }


    @Override
    public void showError(String message) {
        LoadDialog.dismiss(this);
        ToastUtils.showToast(this,message);
    }

    /**
     * 抄报时的弹窗
     */
    private void createHint() {
        CustomDialog.Builder builder=new CustomDialog.Builder(this);
        CustomDialog dialog=builder.setMessage("您已阅读 可在已办列表查看").
                setBackButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                }).setConfirmButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        }).create();
        dialog.show();
    }

    @Override
    public void getAppealDetail(final AppealDetailBean detailBean) {
     if (rowsBean.getAppealState() == 50&&rowsBean.getSourcesWay()==50) {//待办并且是抄告
         createHint();
     }
        LoadDialog.dismiss(this);
        if (detailBean!=null){
            tv_EventName.setText(detailBean.getName());
            tv_HappenGrid.setText(detailBean.getOrg()!=null ? detailBean.getOrg().getOrgName():"未知");
            tv_HappenLocation.setText(detailBean.getOccurrencePlace());
            tv_Data.setText(detailBean.getOccurrenceTime());
            tv_Litigant.setText(detailBean.getLitigant());
            tv_Phone.setText(detailBean.getTelphone());
            tv_AppealSource.setText(detailBean.getAppealSource()!=null ? detailBean.getAppealSource().getDisplayName():"未知");
            tv_AppealType.setText(detailBean.getAppealType()!=null ?
                    detailBean.getAppealType().getDomainName()+"-"+detailBean.getAppealType().getDictName():"未知");
            tv_Describe.setText(detailBean.getDescription());
            if (detailBean.getFiles()!=null&&detailBean.getFiles().size()>0){
                rvImageRV.setLayoutManager(new LinearLayoutManager(context, LinearLayout.HORIZONTAL, false));
                final PictureAdapter pictureAdapter = new PictureAdapter(context, detailBean.getFiles());
                rvImageRV.setAdapter(pictureAdapter);
                pictureAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(final BaseQuickAdapter adapter, View view, final int position) {
                    String type = detailBean.getFiles().get(position).getSuffixType();
                    String path = detailBean.getFiles().get(position).getPath();
                    if(".mp4".equalsIgnoreCase(type)){
                        startActivity(new Intent(AppealDetailActivity.this, VideoPlayerActivity.class).putExtra(
                                "path",path));
                    }else if(".wav".equalsIgnoreCase(type) || ".amr".equalsIgnoreCase(type)){
                        int isPlayingPostion = 0;
                        for(int i = 0 ; i < detailBean.getFiles().size() ; i++){
                            if (detailBean.getFiles().get(i).isPlay){
                                isPlayingPostion = i;
                                break;
                            }
                        }
                        if (player == null){
                            player = new Player(AppealDetailActivity.this);
                        }
                        if (player.isPlay() && isPlayingPostion == position){
                            player.stop();
                            detailBean.getFiles().get(isPlayingPostion).isPlay = false;
                            adapter.notifyDataSetChanged();
                        }else if (player.isPlay() && isPlayingPostion != position){
                            player.stop();
                            detailBean.getFiles().get(isPlayingPostion).isPlay = false;
                            detailBean.getFiles().get(position).isPlay = true;
                            adapter.notifyDataSetChanged();
                            player.playUrl(path,null, new Player.OnCompletionListener() {
                                @Override
                                public void onPlayComplet() {
                                    detailBean.getFiles().get(position).isPlay = false;
                                    pictureAdapter.notifyDataSetChanged();
                                }
                            });
                        }else {
                            detailBean.getFiles().get(position).isPlay = true;
                            adapter.notifyDataSetChanged();
                            player.playUrl(path,null,new Player.OnCompletionListener() {
                                @Override
                                public void onPlayComplet() {
                                    detailBean.getFiles().get(position).isPlay = false;
                                    pictureAdapter.notifyDataSetChanged();
                                }
                            });
                        }
                    }else if (".jpeg".equalsIgnoreCase(type)||".png".equalsIgnoreCase(type)||
                            ".jpg".equalsIgnoreCase(type)){
                        startActivity(new Intent(AppealDetailActivity.this, ImagePlayActivity.class).putExtra(
                                "path", path));
                    }else {
                        ToastUtils.showToast(AppealDetailActivity.this,"不能查看文件");
                    }
                }
            });
            }
        }
    }
}
